package com.atguigu.srb.core.controller.api;


import com.atguigu.srb.base.result.ResponseVo;
import com.atguigu.srb.base.util.JwtUtils;
import com.atguigu.srb.core.pojo.entity.Lend;
import com.atguigu.srb.core.pojo.vo.LendInfoVO;
import com.atguigu.srb.core.pojo.vo.LendSearchVO;
import com.atguigu.srb.core.service.LendService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 标的准备表 前端控制器
 * </p>
 *
 * @author Atguigu
 * @since 2023-03-25
 */
@RestController
@RequestMapping("/api/core/lend")
public class ApiLendController {
    @Resource
    LendService lendService;
    @ApiOperation("判断登录用户是否为标的的借款人")
    @GetMapping("isBorrower/{id}")
    public ResponseVo isBorrower(@RequestHeader(value = "token",required = false)
                                         String token,
                                 @PathVariable("id")Long id){
        Long userId = JwtUtils.getUserId(token);

        int count = lendService.count(Wrappers.lambdaQuery(Lend.class)
                .eq(Lend::getId, id)
                .eq(Lend::getUserId, userId));
        return ResponseVo.ok().data("flag",count==1);//true表示 标的是登录用户的标的
    }

    @ApiOperation("计算投资收益")
    @GetMapping("/income/{amount}/{lendYearRate}/{period}/{returnMethod}") // 投资金额、标的年化率、期数、还款方式
    public ResponseVo income(@PathVariable("amount")BigDecimal amount,
                             @PathVariable("lendYearRate")BigDecimal lendYearRate,
                             @PathVariable("period")Integer period,
                             @PathVariable("returnMethod")Integer returnMethod){
        BigDecimal income = lendService.income(amount,lendYearRate,period,returnMethod);
        return ResponseVo.ok().data("income",income);
    }
    @ApiOperation("查询标的详情")
    @GetMapping("{id}")
    public ResponseVo getById(@PathVariable("id")Long id){
        LendInfoVO lendInfoVO = lendService.getLendInfoVOById(id);
        return ResponseVo.ok().data("item",lendInfoVO);
    }


    @ApiOperation("会员条件查询标的列表")
    @GetMapping("list/{pageNum}/{pageSize}")
    public ResponseVo list(LendSearchVO lendSearchVO,
                           @PathVariable("pageNum")Integer pageNum,
                           @PathVariable("pageSize")Integer pageSize){//pojo入参
        Page<Lend> page = lendService.getLendsByCondition(lendSearchVO,pageNum,pageSize);
        return ResponseVo.ok().data("page",page);

    }
}

